package com.lyj.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.content.Media;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 多模态
 */
@RestController
public class MultiModalController {

    @Autowired
    ChatModel chatModel ;

    /**
     * 接收用户输入文本，并可以处理其他媒体消息
     * @param msg
     * @return
     */
    @RequestMapping("/ai/multimodal")
    public String multiModal(@RequestParam(value = "msg",defaultValue = "讲个笑话") String msg) {
        Resource png = new ClassPathResource("/static/学籍在线验证报告.png");

        UserMessage userMessage = new UserMessage(
                msg, // content 接收用户输入
                new Media(MimeTypeUtils.IMAGE_PNG, png)); // media

        ChatResponse response = chatModel.call(new Prompt(userMessage));
        return response.getResult().getOutput().getText();
    }

    @RequestMapping("/ai/multimodal2")
    public String multimodal2() {
        String response = ChatClient.create(chatModel).prompt()
                .user(u -> u.text("Explain what do you see on this picture?")
                        .media(MimeTypeUtils.IMAGE_JPEG, new ClassPathResource("/static/病毒.jpg")))
                .call()
                .content();
        return response;
    }
}
